import { Body, Controller, Delete, Get, HttpService, Post, Put, Query, Req, Res } from '@nestjs/common';
import { Response } from 'express';
import { ApiResult, ApiResultCode } from '../../common/api-result';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { PluginAliyunSLB } from './plugin-aliyun-slb.entity';
import { Page } from '../../common/page';
import { EnvDto } from '../../env/env.dto';
import { ServerDto } from '../../server/server.dto';
import { PluginAliyunPolardb } from './plugin-aliyun-polardb.entity';

@Controller()
export class PluginAliyunPolardbController {

  constructor(
    @InjectRepository(PluginAliyunPolardb)
    private pluginAliyunPolardbRepository: Repository<PluginAliyunPolardb>,
  ) {
  }

  /**
   * 添加阿里云SLB
   * @param dto
   * @param res
   */
  @Post('/api/plugin/aliyun-polardb/save')
  async save(@Body() dto: PluginAliyunPolardb, @Res() res: Response) {
    const ar = new ApiResult();
    if (!dto.instanceId) {
      return res.json(ApiResultCode['2']('instanceId 不能为空'));
    }
    let polardb = await this.pluginAliyunPolardbRepository.findOne({ instanceId: dto.instanceId });
    if (!polardb) {
      polardb = new PluginAliyunPolardb();
    }
    polardb.instanceId = dto.instanceId;
    polardb.instanceName = dto.instanceName;
    polardb.isMonitor = dto.isMonitor;
    await this.pluginAliyunPolardbRepository.save(polardb);
    return res.json(ar);
  }

  /**
   * 阿里云SLB 更新监控状态
   * @param dto
   * @param res
   */
  @Put('/api/plugin/aliyun-polardb/updateMonitor')
  async updateMonitor(@Body() dto: PluginAliyunPolardb, @Res() res: Response) {
    const ar = new ApiResult();
    if (!dto.instanceId) {
      return res.json(ApiResultCode['2']('instanceId 不能为空'));
    }
    const polardb = await this.pluginAliyunPolardbRepository.findOne({ instanceId: dto.instanceId });
    if (!polardb) {
      return res.json(ApiResultCode['3']('实例id 不存在'));
    }
    polardb.isMonitor = dto.isMonitor;
    await this.pluginAliyunPolardbRepository.save(polardb);
    return res.json(ar);
  }

  /**
   * 阿里云SLB 删除
   * @param dto
   * @param res
   */
  @Delete('/api/plugin/aliyun-polardb/delete')
  async delete(@Body() dto: PluginAliyunPolardb, @Res() res: Response) {
    const ar = new ApiResult();
    if (!dto.instanceId) {
      return res.json(ApiResultCode['2']('instanceId 不能为空'));
    }
    const polardb = await this.pluginAliyunPolardbRepository.findOne({ instanceId: dto.instanceId });
    if (!polardb) {
      return res.json(ApiResultCode['3']('实例id 不存在'));
    }
    await this.pluginAliyunPolardbRepository.delete(polardb.id);
    return res.json(ar);
  }

  @Get('/api/plugin/aliyun-polardb/page')
  async list(@Query() dto: PluginAliyunPolardb, @Req() req, @Res() res: Response) {
    const ar = new ApiResult();
    const page = new Page();
    await this.pluginAliyunPolardbRepository.createQueryBuilder('s')
      .skip(PluginAliyunPolardb.getOffset(PluginAliyunPolardb.getPageNo(dto.pageNo), ServerDto.getPageSize(dto.pageSize)))
      .take(PluginAliyunPolardb.getPageSize(dto.pageSize))
      .getManyAndCount()
      .then(value => {
        page.data = value[0];
        page.total = value[1];
      });
    page.pageNo = dto.pageNo;
    page.pageSize = dto.pageSize;
    // 分页总数，总记录数 / 页条数，当存在小数点，使用了 Math.ceil 直接网上取整数
    page.totalPage = Math.ceil(page.total / page.pageSize);
    ar.result = page;
    return res.json(ar);
  }

}